package dp.mobile.eden;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.opengl.GLSurfaceView;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import dp.mobile.entity.Entity;
import dp.mobile.entity.Node;
import dp.mobile.entity.generator.Generator;
import dp.mobile.entity.generator.GeneratorFactory;
import dp.mobile.entity.tower.Tower;
import dp.mobile.entity.tower.TowerData;
import dp.mobile.entity.tower.TowerFactory;
import dp.mobile.game.Game;
import dp.mobile.game.GameEventListener;
import dp.mobile.game.GameTouchListener;
import dp.mobile.game.Score;
import dp.mobile.glwrapper.GameRenderer;
import dp.mobile.panel.PanelTouchListener;
import dp.mobile.system.SoundManager;
import dp.mobile.system.Utilities;

public abstract class GameActivityBase extends Activity implements GameEventListener {
	private static final String TAG = "Game Act Base";
	protected static final int RCODE_PAUSE_ACT = 1;
	public static final String LEVEL_EXTRAS = "LEVEL";
	public static final String SCORE_EXTRAS = "SCORE";
	public static final String EXTRA_PLAYER_STAT = "PLAYER_STAT";
	
	public void showTutorialPanel(String pText) {
		mTutorialTxtView.setText(pText);
		mTutorialPanel.setVisibility(View.VISIBLE);
	}
	
	public void hideTutorialPanel() {
		mTutorialPanel.setVisibility(View.GONE);
	}
	
	protected void initComp(){
		//Info Panel
		mInfoPanel			= (RelativeLayout) findViewById(R.id.info_panel);
		mDefenseCount		= (TextView) findViewById(R.id.defense_amount);
		mElectricityCount	= (TextView) findViewById(R.id.electricity_count);
		mComponent0Count	= (TextView) findViewById(R.id.component0_count);
		mComponent1Count	= (TextView) findViewById(R.id.component1_count);
		mComponent2Count	= (TextView) findViewById(R.id.component2_count);
		
		//Tutorial Panel
		mTutorialPanel		= (LinearLayout) findViewById(R.id.tutorial_panel);
		mTutorialTxtView	= (TextView) findViewById(R.id.tutorial_text);
		
		//Win Panel
		mWinPanel			= (RelativeLayout) findViewById(R.id.win_panel);
		mWinImg				= (ImageView) findViewById(R.id.win_img);
		
		//Transparent Panel
		mTransparentPanel	= (RelativeLayout) findViewById(R.id.transparent_panel);
		mTransparentPanelAnimStart = AnimationUtils.loadAnimation(this, R.anim.transparent_panel_anim_start);
		mTransparentPanelAnimFinish = AnimationUtils.loadAnimation(this, R.anim.transparent_panel_anim_finish);
		
		//Sliding layout
		mSlidingLayout	= (RelativeLayout) findViewById(R.id.sliding_layout);
		mSlideRight		= AnimationUtils.loadAnimation(this, R.anim.interface_slideright);
		mSlideLeft		= AnimationUtils.loadAnimation(this, R.anim.interface_slideleft);
		mLeftLayout		= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
		mRightLayout	= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
		mLeftLayout.addRule(RelativeLayout.LEFT_OF, mTransparentPanel.getId());
		mRightLayout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		
		//Top Panel
		//mTopPanel	= (LinearLayout) findViewById(R.id.top_panel);
		mMainImage	= (Button) findViewById(R.id.main_image);
		mTextInfo1	= (TextView) findViewById(R.id.info1_txtview);
		mTextInfo2	= (TextView) findViewById(R.id.info2_txtview); 
		mTextInfo3	= (TextView) findViewById(R.id.info3_txtview);;
		
		//Middle Panel
		//mMiddlePanel	= (RelativeLayout) findViewById(R.id.middle_panel);
		mAction1		= (Button) findViewById(R.id.action1);
		mAction2		= (Button) findViewById(R.id.action2); 
		mAction3		= (Button) findViewById(R.id.action3); 
		mAction4		= (Button) findViewById(R.id.action4);
		
		mDescription1	= (TextView) findViewById(R.id.description1);
		mDescription2	= (TextView) findViewById(R.id.description2);
		mDescription3	= (TextView) findViewById(R.id.description3);
		mDescription4	= (TextView) findViewById(R.id.description4);
		
		mButton1 = (Button) findViewById(R.id.button1);
		mButton2 = (Button) findViewById(R.id.button2);
		mButton3 = (Button) findViewById(R.id.button3);
		mButton4 = (Button) findViewById(R.id.button4);
		
		//Bottom Panel
		//mBottomPanel	= (RelativeLayout) findViewById(R.id.bottom_panel);
		mDismantleBtn	= (Button) findViewById(R.id.dismantle_btn);
		mClosePanelBtn	= (Button) findViewById(R.id.close_panel_btn);
		
		//Pause panel
		mBlackScreen		= (LinearLayout) findViewById(R.id.black_screen);
		mPausePanel			= (LinearLayout) findViewById(R.id.pause_panel);
		mResumeGameButton	= (Button) findViewById(R.id.resume_game_button);
		mEndGameButton		= (Button) findViewById(R.id.end_game_button);
		mSaveGameButton		= (Button) findViewById(R.id.save_game_button);
		mPausePanelAnimStart	= AnimationUtils.loadAnimation(this, R.anim.pause_panel_anim_start);
		mPausePanelAnimFinish	= AnimationUtils.loadAnimation(this, R.anim.pause_panel_anim_finish);

		mTutorialPanel.setVisibility(View.GONE);
		mWinPanel.setVisibility(View.GONE);
		mTransparentPanel.setVisibility(View.GONE);
		mPausePanel.setVisibility(View.GONE);
		mBlackScreen.setVisibility(View.GONE);

		SoundManager.instance(getBaseContext()).stopAllSound();
		SoundManager.instance(getBaseContext()).playBGM(Utilities.BGM_FOLDER + "/game.mp3", true);
		
		PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
		mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
		mWakeLock.acquire();
	}
	
	// assume mPanelTouchListener is instantiated
	protected void setListener(){
		mClosePanelBtn.setOnClickListener(mPanelTouchListener);
		mDismantleBtn.setOnClickListener(mPanelTouchListener);
		mAction1.setOnClickListener(mPanelTouchListener);
		mAction2.setOnClickListener(mPanelTouchListener);
		mAction3.setOnClickListener(mPanelTouchListener);
		mAction4.setOnClickListener(mPanelTouchListener);
		
		mDescription1.setOnClickListener(mPanelTouchListener);
		mDescription2.setOnClickListener(mPanelTouchListener);
		mDescription3.setOnClickListener(mPanelTouchListener);
		mDescription4.setOnClickListener(mPanelTouchListener);
		mButton1.setOnClickListener(mPanelTouchListener);
		mButton2.setOnClickListener(mPanelTouchListener);
		mButton3.setOnClickListener(mPanelTouchListener);
		mButton4.setOnClickListener(mPanelTouchListener);
	}
	
	protected void setPausePanelListeners() {
		mResumeGameButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mGame.onResume();
				mPausePanel.startAnimation(mPausePanelAnimFinish);
				mPausePanel.setVisibility(View.GONE);
				mBlackScreen.setVisibility(View.GONE);
			}
		});
		
		mEndGameButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mGame.endGame();
				mPausePanel.startAnimation(mPausePanelAnimFinish);
				mPausePanel.setVisibility(View.GONE);
				mBlackScreen.setVisibility(View.GONE);
			}
		});
		
		mSaveGameButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mGame.save();
				mSaveGameButton.setText("Game saved");
			}
		});
	}
	
	@Override
	public void onPause() {
		super.onPause();
		mSurface.onPause();
		mGame.onPause();
		mWakeLock.release();
		//GameRenderer.instance().invalidateAllTextures();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		mSurface.onResume();
		mGame.onResume();
		
		mWakeLock.acquire();
	}
	
	@Override
	public void onBackPressed() {
		if (mTransparentPanel.getVisibility() == View.VISIBLE){
			mTransparentPanel.startAnimation(mTransparentPanelAnimFinish);
			mTransparentPanel.setVisibility(View.GONE);
			
			//
			mSlidingLayout.startAnimation(mSlideRight);
			mSlidingLayout.setLayoutParams(mRightLayout);			
		}
		else {
			if (mGame.getPlayer().hasWon())
				onWinPressed(mGame.getLevelName(), mGame.getPlayer().getScore());
			else {
				if (mPausePanel.getVisibility() == View.VISIBLE) {
					mGame.onResume();
					mPausePanel.startAnimation(mPausePanelAnimFinish);
					mPausePanel.setVisibility(View.GONE);
					mBlackScreen.setVisibility(View.GONE);
				} else {
					mGame.onPause();
					mPausePanel.startAnimation(mPausePanelAnimStart);
					mPausePanel.setVisibility(View.VISIBLE);
					mBlackScreen.setVisibility(View.VISIBLE);
				}
			}
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		synchronized (GameRenderer.instance()) {
			// panel touch listener cuma buat dengerin gone / visible nya panel
			mGameTouchListener.onTouch(event);
		}
			
		try {
            Thread.sleep(32);
        } catch (InterruptedException e) {
            // No big deal if this sleep is interrupted.
        }
        
		return true;
	}
	
	protected GLSurfaceView			mSurface;
	protected GameTouchListener		mGameTouchListener;
	protected PanelTouchListener	mPanelTouchListener;
	protected PowerManager.WakeLock mWakeLock;
	
	//Info Panel
	protected RelativeLayout	mInfoPanel;
	protected TextView			mElectricityCount;
	protected TextView			mComponent0Count, mComponent1Count, mComponent2Count;
	protected TextView			mDefenseCount;
	
	//
	protected RelativeLayout				mSlidingLayout;
	protected Animation						mSlideRight;
	protected Animation						mSlideLeft;
	protected RelativeLayout.LayoutParams	mLeftLayout;
	protected RelativeLayout.LayoutParams	mRightLayout;
	
	//Tutorial Panel
	protected LinearLayout		mTutorialPanel;
	protected TextView			mTutorialTxtView;
	
	//Win Panel
	protected RelativeLayout	mWinPanel;
	protected ImageView			mWinImg;
	
	//Transparent Panel
	protected RelativeLayout	mTransparentPanel;
	protected Animation			mTransparentPanelAnimStart, mTransparentPanelAnimFinish;
	
	//Top Panel
	//protected LinearLayout		mTopPanel;
	protected Button			mMainImage;
	protected TextView			mTextInfo1, mTextInfo2, mTextInfo3;
	
	//Middle Panel
	//protected RelativeLayout	mMiddlePanel;
	protected Button			mAction1, mAction2, mAction3, mAction4;
	protected TextView			mDescription1, mDescription2, mDescription3, mDescription4;
	protected Button			mButton1, mButton2, mButton3, mButton4;
	
	//Bottom Panel
	protected RelativeLayout	mBottomPanel;
	protected Button			mDismantleBtn;
	protected Button			mClosePanelBtn;
	
	//Pause panel
	protected LinearLayout		mPausePanel;
	protected LinearLayout		mBlackScreen;
	protected Animation			mPausePanelAnimStart, mPausePanelAnimFinish;
	protected Button			mResumeGameButton;
	protected Button			mSaveGameButton;
	protected Button			mEndGameButton;
	protected int[]				mAvailableTowers = null;

	protected Game				mGame;
	protected Entity			mLasEntity;
	protected Handler			mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.arg1) {
			case ELECTRICITY_CHANGE :
				mElectricityCount.setText("" + msg.arg2);
				break;
			case COMPONENT0_CHANGE :
				mComponent0Count.setText("" + msg.arg2);
				break;
			case COMPONENT1_CHANGE :
				mComponent1Count.setText("" + msg.arg2);
				break;
			case COMPONENT2_CHANGE :
				mComponent2Count.setText("" + msg.arg2);
				break;
			case LIFE_CHANGE :
				mDefenseCount.setText("" + msg.arg2);
				break;
				
			case WIN_PANEL_SHOW :
				mBlackScreen.setVisibility(View.VISIBLE);
				mWinPanel.setVisibility(View.VISIBLE);
				mTransparentPanel.setVisibility(View.GONE);
				mPausePanel.setVisibility(View.GONE);
				
				Log.d(TAG, "Score = " + msg.arg2);
				/// TODO : harusnya ngga di electricity count, bikin suatu tempat buat score
				mDefenseCount.setText("" + msg.arg2);
				System.gc();
				break;
			}
		}
	};
	
	protected static final int ELECTRICITY_CHANGE 	= 0;
	protected static final int LIFE_CHANGE 			= 1;
	protected static final int WIN_PANEL_SHOW 		= 2;
	protected static final int COMPONENT0_CHANGE 	= 3;
	protected static final int COMPONENT1_CHANGE 	= 4;
	protected static final int COMPONENT2_CHANGE 	= 5;
	
	
	protected boolean isGameOver = false;
	
	///GAME EVENT LISTENER
	@Override
	public void onElectricityChange(int currentElect) {
		Message msg = new Message();
		msg.arg1 = ELECTRICITY_CHANGE;
		msg.arg2 = currentElect;
		mHandler.sendMessage(msg);
	}
	
	@Override
	public void onLifeChange(int currentLife) {
		Message msg = new Message();
		msg.arg1 = LIFE_CHANGE;
		msg.arg2 = currentLife;
		mHandler.sendMessage(msg);
	}

	@Override
	public void onComponentChange(int[] components) {
		Message msg = new Message();
		msg.arg1 = COMPONENT0_CHANGE;
		msg.arg2 = components[0];
		mHandler.sendMessage(msg);
		
		msg = new Message();
		msg.arg1 = COMPONENT1_CHANGE;
		msg.arg2 = components[1];
		mHandler.sendMessage(msg);
		
		msg = new Message();
		msg.arg1 = COMPONENT2_CHANGE;
		msg.arg2 = components[2];
		mHandler.sendMessage(msg);
	}

	@Override
	public void onGameOver() {
		if (!isGameOver) {
			if (mGame.getPlayer().getLife() <= 0)
				startActivity(new Intent(this, GameOverAct.class));
			finish();
			isGameOver = true;
		}
	}

	@Override
	public void onWin(int score) {
		Message msg = new Message();
		msg.arg1 = WIN_PANEL_SHOW;
		msg.arg2 = score;
		mHandler.sendMessage(msg);
	}
	
	@Override
	public void onWinPressed(String level, int score) {
		Intent intent1 = new Intent(this, SaveHighScoreAct.class);
		intent1.putExtra(SaveHighScoreAct.LEVEL_NAME_EXTRA, level);
		intent1.putExtra(Score.SCORE_EXTRA, score);
		startActivity(intent1);

		finish();
	}
	
	@Override
	public void onDialog(String level, int wave) {
		Intent intent = new Intent(this, DialogAct.class);
		intent.putExtra("level", level);
		intent.putExtra("wave", wave);
		
		startActivity(intent);
	}
	
	@Override
	public void onPanelShow() {
		if (mTransparentPanel.getVisibility() != View.VISIBLE){
			//Show panel
			mTransparentPanel.startAnimation(mTransparentPanelAnimStart);
			mTransparentPanel.setVisibility(View.VISIBLE);
			
			//Slide
			mSlidingLayout.setLayoutParams(mLeftLayout);
			mSlidingLayout.startAnimation(mSlideLeft);
		}
		
		if (mGame.getPlayer().isSelectedTileValid()) {
			Entity ent = mGame.getEntityAt(mGame.getPlayer().getSelectedTile().x, mGame.getPlayer().getSelectedTile().y);
			
			// refresh view
			mTextInfo1.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
			mMainImage.setBackgroundDrawable(null);
			mMainImage.setVisibility(View.VISIBLE);
			mAction1.setBackgroundDrawable(null);
			mAction2.setBackgroundDrawable(null);
			mAction3.setBackgroundDrawable(null);
			mAction4.setBackgroundDrawable(null);
			mAction1.setVisibility(View.GONE);
			mAction2.setVisibility(View.GONE);
			mAction3.setVisibility(View.GONE);
			mAction4.setVisibility(View.GONE);
			mDismantleBtn.setVisibility(View.GONE);
			mDismantleBtn.setText("Dismantle");
			mDescription1.setVisibility(View.GONE);
			mDescription2.setVisibility(View.GONE);
			mDescription3.setVisibility(View.GONE);
			mDescription4.setVisibility(View.GONE);			
			mButton1.setVisibility(View.GONE);
			mButton2.setVisibility(View.GONE);
			mButton3.setVisibility(View.GONE);
			mButton4.setVisibility(View.GONE);
			mTextInfo2.setVisibility(View.GONE);
			mTextInfo3.setText(null);
			mPanelTouchListener.setPanelLevel(0);
			
			// hide range
			if (mLasEntity != null) {
				if (mLasEntity instanceof Tower) {
					((Tower) mLasEntity).setRangeShowing(false);
				} else if (mLasEntity instanceof Generator) {
					((Generator) mLasEntity).setRangeShowing(false);
				}
			}
			
			boolean isSameSide = ent != null && ent.getPlayer().equals(mGame.getPlayer()); 
			
			// show range
			if (ent != null) {
				mTextInfo1.setText(ent.getName());
				if (ent instanceof Tower) {
					Tower tower = (Tower)ent;
					tower.setRangeShowing(true);
					mMainImage.setBackgroundDrawable(new BitmapDrawable(tower.getData().getIcon()));
					
					BitmapDrawable attackIcon = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.attack));
					mTextInfo2.setVisibility(View.VISIBLE);
					mTextInfo2.setCompoundDrawablesWithIntrinsicBounds(attackIcon, null, null, null);
					mTextInfo2.setText(tower.getDamage() + " + " + tower.getBonusDamage());
					mTextInfo3.setText(tower.getData().getDescription());
					
					if (isSameSide) {
						mDismantleBtn.setVisibility(View.VISIBLE);
						int[] upgrade 	= new int[] { tower.getData().getUpgrade(0), tower.getData().getUpgrade(1), tower.getData().getUpgrade(2) };
						TowerData data 	= null;
						boolean allowed = true;
						
						if (upgrade != null) {
							if (upgrade.length > 0) {
								allowed = true;
								data = TowerFactory.instance().getTowerData(upgrade[0]);
								if (mAvailableTowers != null) {
									allowed = false;
									for (int i = 0; i < mAvailableTowers.length; ++i)
										if (mAvailableTowers[i] == data.getIndex()) {
											allowed = true;
											break;
										}
								}
									
								if (allowed) {
									mTextInfo3.setText("Upgrades");
									mAction1.setBackgroundDrawable(new BitmapDrawable(data.getIcon()));
									mAction1.setVisibility(View.VISIBLE);
									mButton1.setText(data.getName());
									mButton1.setVisibility(View.VISIBLE);
									//mDescription1.setText(data.getName());
									//mDescription1.setVisibility(View.VISIBLE);
								}
							} if (upgrade.length > 1) {
								allowed = true;
								data = TowerFactory.instance().getTowerData(upgrade[1]);
								if (mAvailableTowers != null) {
									allowed = false;
									for (int i = 0; i < mAvailableTowers.length; ++i)
										if (mAvailableTowers[i] == data.getIndex()) {
											allowed = true;
											break;
										}
								}
								
								if (allowed) {
									mAction2.setBackgroundDrawable(new BitmapDrawable(data.getIcon()));
									mAction2.setVisibility(View.VISIBLE);
									mButton2.setText(data.getName());
									mButton2.setVisibility(View.VISIBLE);
									//mDescription2.setText(data.getName());
									//mDescription2.setVisibility(View.VISIBLE);
								}
							} if (upgrade.length > 2) {
								allowed = true;
								data = TowerFactory.instance().getTowerData(upgrade[2]);
								if (mAvailableTowers != null) {
									allowed = false;
									for (int i = 0; i < mAvailableTowers.length; ++i)
										if (mAvailableTowers[i] == data.getIndex()) {
											allowed = true;
											break;
										}
								}
								
								if (allowed) {
									mAction3.setBackgroundDrawable(new BitmapDrawable(data.getIcon()));
									mAction3.setVisibility(View.VISIBLE);
									mButton3.setText(data.getName());
									mButton3.setVisibility(View.VISIBLE);
									//mDescription3.setText(data.getName());
									//mDescription3.setVisibility(View.VISIBLE);
								}
							}
						}
					}
				} else if (ent instanceof Generator) {
					Generator generator = (Generator) ent;
					generator.setRangeShowing(true);
					mMainImage.setBackgroundDrawable(new BitmapDrawable(GeneratorFactory.instance().getIcon()));
					
					if (isSameSide) {
						mAction1.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.component0)));
						mAction2.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.component1)));
						mAction3.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.component2)));
						
						mAction1.setVisibility(View.VISIBLE);
						mAction2.setVisibility(View.VISIBLE);
						mAction3.setVisibility(View.VISIBLE);
						
						mTextInfo3.setText("Upgrades");
						mTextInfo3.setVisibility(View.VISIBLE);
						
						mButton1.setText("Power");
						mButton2.setText("Range");
						mButton3.setText("Voltage");

						mButton1.setVisibility(View.VISIBLE);
						mButton2.setVisibility(View.VISIBLE);
						mButton3.setVisibility(View.VISIBLE);
						
						mDismantleBtn.setVisibility(View.VISIBLE);
					}
				} else if (ent instanceof Node) {
					/// TODO : show range tower
					if (isSameSide && ((Node)ent).isOnline()) {
						//mAction1.setBackgroundDrawable(new BitmapDrawable(TowerFactory.instance().getBasicTower().getIcon()));
						//mAction1.setVisibility(View.VISIBLE);
						TowerData towerData = TowerFactory.instance().getBasicTower();
						mMainImage.setBackgroundDrawable(new BitmapDrawable(towerData.getIcon()));
						mTextInfo1.setText(towerData.getName());
						
						mPanelTouchListener.setPanelLevel(1);
						
						mAction1.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.electricity)));
						mAction2.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.component0)));
						mAction3.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.component1)));
						mAction4.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.component2)));
						
						mAction1.setVisibility(View.VISIBLE);
						mAction2.setVisibility(View.VISIBLE);
						mAction3.setVisibility(View.VISIBLE);
						mAction4.setVisibility(View.VISIBLE);

						mDescription1.setVisibility(View.VISIBLE);
						mDescription2.setVisibility(View.VISIBLE);
						mDescription3.setVisibility(View.VISIBLE);
						mDescription4.setVisibility(View.VISIBLE);
						
						mTextInfo3.setText("Requirements");
						mTextInfo3.setVisibility(View.VISIBLE);
						
						mDescription1.setText(" x 5");
						mDescription2.setText(" x 1");
						mDescription3.setText(" x 1");
						mDescription4.setText(" x 1");
						
						mButton1.setVisibility(View.GONE);
						mButton2.setVisibility(View.GONE);
						mButton3.setVisibility(View.GONE);
						mButton4.setVisibility(View.GONE);
						
						mDismantleBtn.setVisibility(View.VISIBLE);
						mDismantleBtn.setText("Build");

						//mDescription1.setText("Build Basic Tower");
						//mDescription1.setVisibility(View.VISIBLE);
					}
				}
			} else {
				//mMainImage.setVisibility(View.GONE);
				//mTextInfo1.setText("Tanah Kosong");
				/// TODO : show range generator
				if (mGame.isTileBuildable(mGame.getPlayer().getSelectedTile().x, mGame.getPlayer().getSelectedTile().y)) {
					BitmapDrawable generatorIcon = new BitmapDrawable(GeneratorFactory.instance().getIcon());
					//mAction1.setBackgroundDrawable(generatorIcon);
					//mAction1.setVisibility(View.VISIBLE);
					mMainImage.setVisibility(View.VISIBLE);
					mMainImage.setBackgroundDrawable(generatorIcon);
					mTextInfo1.setText(GeneratorFactory.instance().getName());
					
					mPanelTouchListener.setPanelLevel(1);
					
					mAction1.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.electricity)));
					mAction2.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.component0)));
					mAction3.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.component1)));
					mAction4.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.component2)));
					
					mTextInfo3.setText("Requirement");
					mTextInfo3.setVisibility(View.VISIBLE);
					
					mAction1.setVisibility(View.VISIBLE);
					
					mDescription1.setText("x " + GeneratorFactory.instance().getBaseBuildCost());

					mDescription1.setVisibility(View.VISIBLE);
					
					mDismantleBtn.setVisibility(View.VISIBLE);
					mDismantleBtn.setText("Build");
					
					//mDescription1.setText("Build Generator");
					//mDescription1.setVisibility(View.VISIBLE);
				}
			}
			
			mLasEntity = ent;
		}
	}
}
